package com.zz.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
        // 2.设置参数
        factory.setHost("8.140.201.46");
        factory.setPort(5672);
        factory.setVirtualHost("zz");
        factory.setUsername("admin");
        factory.setPassword("admin");
        // 3.创建连接
        Connection connection = factory.newConnection();
        // 4.创建通道
        Channel channel = connection.createChannel();
        // 5.声明队列
        channel.queueDeclare("test001",true,false,false,null);
        // 6.发送消息
        for (int i = 0; i < 10; i++) {
            String message = "Hello World RabbitMQ!" + i;
            channel.basicPublish("", "test001", null, message.getBytes());
        }

        // 7.关闭信道
        channel.close();
        // 8.关闭连接
        connection.close();
    }
}
